//
//  StretchView.m
//  <15.1>  DrawingFun
//
//  Created by chen on 16/9/18.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "StretchView.h"

@interface StretchView (){

    NSPoint _downPoint ;
    NSPoint _currentPoint ;

}

@end


@implementation StretchView


/**************/

- (void)setImage:(NSImage *)image {
    
    _image = image ;
    
    //使得用户即时没有拖动鼠标，仍然能够看到图像
    NSSize imageSize = [image size];
    _downPoint = NSZeroPoint ;
    _currentPoint.x = _downPoint.x + imageSize.width ;
    _currentPoint.y = _downPoint.y + imageSize.height ;
    
    [self setNeedsDisplay:YES];
}


- (void)setOpacity:(CGFloat)opacity{
    
    _opacity = opacity  ;
    
    [self setNeedsDisplay:YES];
}


/*******根据用户的拖拽改变图片的尺寸和位置*******/


/**
 *  mouseDown产生鼠标_downPoint
 */
- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint p = [theEvent locationInWindow];
    //NSLog(@"mouseDown %@", NSStringFromPoint(p)); //貌似是这个window的坐标
    
    _downPoint = [self convertPoint:p fromView:nil]; //从相对于window的坐标系转换到self的坐标系
    _currentPoint = _downPoint ;
    [self setNeedsDisplay:YES];
}


- (void)mouseDragged:(NSEvent *)theEvent
{
    NSPoint p =[theEvent locationInWindow];
    _currentPoint  = [self convertPoint:p fromView:nil];
    
    [self autoscroll:theEvent];
    
    [self setNeedsDisplay:YES];
}

- (void)mouseUp:(NSEvent *)theEvent
{
    NSPoint p = [theEvent locationInWindow];
    _currentPoint  = [self convertPoint:p fromView:nil];
    [self setNeedsDisplay:YES];
}

- (NSRect)currentRect
{
    float minX = MIN(_downPoint.x, _currentPoint.x);
    float maxX = MAX(_downPoint.x, _currentPoint.x);
    float minY = MIN(_downPoint.y, _currentPoint.y);
    float maxY = MAX(_downPoint.y, _currentPoint.y);
    return NSMakeRect(minX, minY, maxX-minX, maxY-minY);
}


/**************/
- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self customInit];
        
    }
    return self;
}

- (void)awakeFromNib{
    [self customInit];
}

- (void)customInit{
    
    //初始为1
    [self willChangeValueForKey:@"opacity"];
    _opacity = 1 ;
    [self didChangeValueForKey:@"opacity"];

    
    //产生随机数生成器
    srand((unsigned)time(NULL));
    
    //创建路径对象
    _path = [NSBezierPath bezierPath];
    [_path setLineWidth:3.0];
    
    
    NSPoint p = [self randomPoint];
    
    [_path moveToPoint:p];
    
    for (int i = 0; i <15 ; i++) {
        p = [self randomPoint];
        [_path lineToPoint:p];
    }
    
    [_path closePath];
}


- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    NSRect bounds = [self bounds];
    [[NSColor greenColor]setFill];
    [NSBezierPath fillRect:bounds];
    
    //用白色绘路径
    [[NSColor whiteColor]set];
//    [_path stroke];
    [_path fill];
    
    if (_image != nil) {
        //将图片组合到视图中，fraction决定透明度
        NSRect imageRect ;
        imageRect.origin = NSZeroPoint ;
        imageRect.size = [_image size];
        NSRect drawingRect = [self currentRect] ;
        [_image drawInRect:drawingRect
                  fromRect:imageRect
                 operation:NSCompositeSourceOver
                  fraction:_opacity];
        
    }
    
}

- (NSPoint)randomPoint{
    
    NSPoint result ;
    NSRect r = [self bounds];
    
    result.x = r.origin.x + random()%(int)r.size.width ;
    result.y = r.origin.y + random()%(int)r.size.height ;
    
    return result ;
}



//- (BOOL)isFlipped{
//    return  YES ; 
//}



@end



//更新方法 setNeedDisplay

// CGRect 是有origin和size两个成员对象的结构体。

// 与iOS不同的是坐标原点在左下角